﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Subfaculty.Common.Interfaces.Services;
using Subfaculty.WebApplication.Translators;
using Subfaculty.Common.Entities;
using Subfaculty.WebApplication.ViewModels;
using Telerik.Web.Mvc;
using Microsoft.Practices.ServiceLocation;

namespace Subfaculty.WebApplication.Controllers
{
	public class StudyYearController : Controller
	{
		private IStudyYearRepository _studyYearRepository;
		private StudyYearTranslator _studyYearTranslator;

		public StudyYearController()
		{
			_studyYearRepository = ServiceLocator.Current.GetInstance<IStudyYearRepository>();
			_studyYearTranslator = new StudyYearTranslator();
		}
		//
		// GET: /StudyYear/

		public ActionResult Index()
		{
			var studyYearsPE = _studyYearTranslator.Translate(_studyYearRepository.GetAll());

			return View(studyYearsPE);
		}

		[GridAction]
		public ActionResult _Index()
		{
			var studyYearPEList = _studyYearTranslator.Translate(_studyYearRepository.GetAll());

			return View("Index",new GridModel(studyYearPEList));
		}

		[GridAction]
		public ActionResult SelectAjaxEditing()
		{

			var studyYearPe = _studyYearTranslator.Translate(_studyYearRepository.GetAll());

			return View("Index", new GridModel(studyYearPe));
		}

		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult SaveAjaxEditing(int id)
		{
			StudyYear studyYear = _studyYearRepository.Get(id);
			StudyYearViewModel studyYearPE = _studyYearTranslator.Translate(studyYear);

			TryUpdateModel(studyYearPE);
			studyYearPE.EndDate = studyYearPE.StartDate + 1;
			_studyYearRepository.Save(_studyYearTranslator.TranslateBack(studyYearPE, studyYear));

			var studyYearPeList = _studyYearTranslator.Translate(_studyYearRepository.GetAll());
			return View("Index", new GridModel(studyYearPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult InsertAjaxEditing()
		{
			StudyYearViewModel pe = new StudyYearViewModel();

			//Perform model binding (fill the product properties and validate it).
			if (TryUpdateModel(pe))
			{
				StudyYear studyYear = new StudyYear();

				pe.EndDate = pe.StartDate + 1;
				//The model is valid - insert the product.
				_studyYearRepository.Save(_studyYearTranslator.TranslateBack(pe, studyYear));
			}
			//Rebind the grid
			var disciplinesPeList = _studyYearTranslator.Translate(_studyYearRepository.GetAll());
			return View("Index", new GridModel(disciplinesPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		
		[GridAction]
		public ActionResult DeleteAjaxEditing(int id)
		{
			StudyYear studyYear = _studyYearRepository.Get(id);
			if (studyYear != null)
			{
				_studyYearRepository.Delete(studyYear);
			}

			var studyYearPeList = _studyYearTranslator.Translate(_studyYearRepository.GetAll());
			return View("Index", new GridModel(studyYearPeList));
		}
	}
}
